export const formatTime = (timeInSeconds) => {
    const hours = Math.floor(timeInSeconds / 3600); // 获取小时
    const minutes = Math.floor((timeInSeconds % 3600) / 60); // 获取分钟
    const seconds = Math.floor(timeInSeconds % 60); // 获取秒数
    const pad = (num) => String(num).padStart(2, '0');
    return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
};
// 上传
export const uploadFile = (filePath) => {
    return new Promise((resolve, reject) => {
        uni.uploadFile({
            url: 'http://zhangyu.aizhangyu.cn/api/common/upload',
            filePath: filePath,
            name: 'file',
            header: {
                token: uni.getStorageSync('USER_TOKEN_INFOS').token
            },
            success: (uploadFileRes) => {
                let srres = JSON.parse(uploadFileRes.data);
                console.log(srres);
                if (srres.code == 1) {
                    resolve(srres.data.url);
                } else {
                    reject('失败');
                }
            },
            fail: (error) => {
                reject('失败');
                console.error('上传失败', error);
            }
        });
    });
};

//查找
export const includeListGetItem = (list = [], id, type = '') => {
    const index = list.findIndex((item) => {
        if (typeof type === 'object') {
            return item.id === id;
        }
        return item === id;
    });
    return index;
};
// 数字换汉字
export const numberToChinese = (num) => {
    const chineseNumbers = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
    const unit = ['', '十', '百', '千']; // 支持十位、百位等
    let result = '';
    const strNum = num.toString();

    for (let i = 0; i < strNum.length; i++) {
        const digit = Number(strNum[i]);
        const position = strNum.length - i - 1;
        result += chineseNumbers[digit];
        if (digit !== 0 && position > 0) {
            result += unit[position];
        }
    }

    // 处理特殊情况
    result = result.replace(/一十/g, '十'); // 比如 "一十" 变成 "十"
    return result;
};
